import { ref } from 'vue'
import { typeSave } from '@/api/goodsSet'
import { UpdateCommodityTypeFrom } from './types'

export default () => {
  const updateCommodityTypeForm = ref<UpdateCommodityTypeFrom>({
    id: '',
    name: '',
    parentId: '',
    status: false,
    visibilityType: 1,
  })

  const updateCommodityTypeLoading = ref(false)

  const updateCommodityType = async () => {
    updateCommodityTypeLoading.value = true
    const res = await typeSave(updateCommodityTypeForm.value)
    updateCommodityTypeLoading.value = false
    return res.data as { code: number; data: object; message: string; success: boolean }
  }

  const resetUpdateCommodityTypeForm = () => {
    updateCommodityTypeForm.value = {
      id: '',
      name: '',
      parentId: '',
      status: false,
      visibilityType: 1,
    }
  }

  return {
    updateCommodityTypeForm,
    updateCommodityTypeLoading,
    updateCommodityType,
    resetUpdateCommodityTypeForm,
  }
}
